package org.pan.service.impl;

import org.hibernate.criterion.DetachedCriteria;
import org.pan.domain.PageResult;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by Administrator on 2015/11/11.
 */
public abstract class AbstractService extends HibernateDaoSupport{

    /**
     * 创建分页查询
     * @param page 当前页码
     * @param pageSize 当页显示数量
     * @param findCriteria 查询记录Criteria
     * @param countCriteria 统计数量Criteria
     * @return 分页对象
     */
    public PageResult createPageResult(Integer page,Integer pageSize,DetachedCriteria findCriteria,DetachedCriteria countCriteria){
        Integer firstResult = (page - 1) * pageSize;
        Integer maxResult = pageSize;

        List<?> findList = getHibernateTemplate().findByCriteria(findCriteria, firstResult, maxResult);
        List<?> countList = getHibernateTemplate().findByCriteria(countCriteria);

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);
        pageResult.setRows(findList);
        pageResult.setTotal((Integer) countList.get(0));
        return pageResult;
    }

}
